package net.jcip.examples.vehicles;

import net.jcip.examples.vehicles.point.ImmutablePoint;
import net.jcip.examples.vehicles.point.MutablePoint;
import net.jcip.examples.vehicles.point.Point;
import net.jcip.examples.vehicles.point.SafePoint;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author nulls.narik
 */
public class PublishingVehicleTracker {

    private final ConcurrentMap<String, SafePoint> locations;
    private final Map<String, SafePoint> unmodifiableMap;

    public PublishingVehicleTracker(Map<String, Point> points) {
        locations = new ConcurrentHashMap<String, SafePoint>();
        for (String key : points.keySet()) {
            locations.put(key, new SafePoint(points.get(key)));
        }
        unmodifiableMap = Collections.unmodifiableMap(locations);
    }

    public SafePoint getLocation(String name) {
        return locations.get(name);
    }

    public void setLocation(String name, double x, double y) {
        if (!locations.containsKey(name)) {
            throw new IllegalArgumentException("No such ID: " + name);
        }
        locations.get(name).set(x, y);
    }

    public Map<String, SafePoint> getLocations() {
        return unmodifiableMap;
    }

}
